//用于前端直接解析 vue文件...
define(['escodegen','esprima','lessc'],function(escodegen,esprima,lessc){  
	var loadParam=function(model,fn){
		var param=[];
		var paramData=[];
		var loadDefine=[];
		for(var i=0;i<model.param.length;i++){
			loadDefine.push(model.param[i].source);
		} 
		require(loadDefine,function(){
			var _arguments=arguments;
			for(var i=0;i<model.param.length;i++){
				var value=_arguments[i];
				for(var q=0;q<model.param[i].name.length;q++){
					param.push(model.param[i].name[q]); 
					if(model.param[i].type=='ImportDefaultSpecifier'){
						paramData.push(value);
					}else{
						var name=model.param[i].name[q];
						paramData.push(value[name]); 
					}
				}
			}
			fn(param,paramData);
			 
		});
		 
	}
	var newFunction=function(model){
		return function(fn){  
			loadParam(model,function(param,paramData){
				 
				var module={};
				param.push("module"); 
				paramData.push(module);
				var _fn=new Function(param,model.code);
				_fn.apply(this,paramData); 
				fn(module.exports);
				
			}) 
		}
	} 
	var handController=function(code){
		var _code=code;
		var param=[]; 
		var ast=esprima.parse(code,{sourceType:"module"});
		var list=[];
		for(var i=0;i<ast.body.length;i++){
			var item=ast.body[i];
			if(item.type!='ImportDeclaration'){
				list.push(item);
			}else{ 
				var name=[];
				for(var q=0;q<item.specifiers.length;q++){
					name.push(item.specifiers[q].local.name); 
				}
				param.push({
					type:item.specifiers[0].type,
					source:item.source.value,
					name:name
				})
				 
			}
		} 
		ast.body=list;   
		return {
			param:param,
			code:escodegen.escodegen.generate(ast)
		}
	}
	var handCss=function(style,name,fn){ 
		var parser = new(less.Parser)({
		    filename: name,
		});    
		parser.parse(style, function (err, css) { 
		    if (err) {
		        if (typeof console !== 'undefined' && console.error) {
		            console.error(err);
		        }
		    } else { 
				fn(css.toCSS());
		    }  
		});	
	}
	var appendStyle=function(temp,css){
		if(css&&css.length>0){ 
			if(temp.indexOf("</")!=-1){
				var begin=temp.substr(0,temp.lastIndexOf("</"));
				var end=temp.substr(temp.lastIndexOf("</"));
				temp=begin;
				temp+="<style>";
				temp+=css;
				temp+="</style>"; 
				temp+=end; 
				
			}
		}
		return temp;
	}
	var getView=function(body,tagName){
		if(body.indexOf("<"+tagName)!=-1){ 
			return body.substring(body.indexOf("<"+tagName)+tagName.length+2,
			
			body.lastIndexOf("</"+tagName)) ; 
		}
	}
	var load=function(fn,path){ 
		var vuePath=path;
		require(["text!"+vuePath],function(view){   
			var template=getView(view,"template");  
			var controller=getView(view,"script");  
			var style=getView(view,"style");   
			var handData=handController(controller); 
			var el=document.createElement("template");
			el.innerHTML=template;
			if(style){
				handCss(style,path,function(css){  
					template=appendStyle(template,css);
					newFunction(handData)(function(vue){
						vue.template=template;   
						fn(vue);
					}); 
				}); 
			}else{
				newFunction(handData)(function(vue){
					vue.template=template;
					fn(vue);
				}); 
			}
			 
		})  
	}
	return	{
		load:function(name, req, onLoad, config){ 
			load(function(data){ 
				onLoad(data); 
			},name);
			
		}
	}
})
